/*/src/utils/themeColorClient.js*/
import client from "webpack-theme-color-replacer/client";
import forElementUI from "webpack-theme-color-replacer/forElementUI";
// 注意自己项目里的引入路径

import appConfig from "../config/config";

let basicColor = {
  primary: "#409EFF",
  success: "#67C23A",
  warning: "#E6A23C",
  danger: "#F56C6C",
  info: "#909399",
  white: "#FFFFFF",
  black: "#626b7d"
};
let objcolors = []
  .concat(forElementUI.getElementUISeries(basicColor.primary))
  .concat(forElementUI.getElementUISeries(basicColor.success))
  .concat(forElementUI.getElementUISeries(basicColor.warning))
  .concat(forElementUI.getElementUISeries(basicColor.danger))
  .concat(forElementUI.getElementUISeries(basicColor.info))
  .concat(forElementUI.getElementUISeries(basicColor.white))
  .concat(forElementUI.getElementUISeries(basicColor.black));

let basicColors = {
  primary: "#409EFF",
  success: "#67C23A",
  warning: "#E6A23C",
  danger: "#F56C6C",
  info: "#909399",
  white: "#FFFFFF",
  black: "#626b7d"
};

// 动态切换主题色
export function changeThemeColor(newColor, Series) {
  basicColors[Series] = newColor;
  let newS = [];
  Object.keys(basicColors).forEach(e => {
    newS = newS.concat(forElementUI.getElementUISeries(basicColors[e]));
  });
  var options = {
    newColors: newS,
    oldColors: objcolors
  };
  return client.changer.changeColor(options).then(() => {
    localStorage.setItem(Series, newColor);
  });
}

export function resetTheme() {
  Object.keys(appConfig.theme).forEach(key => {
    basicColors[key] = appConfig.theme[key];
    changeThemeColor(basicColors[key], key);
  });
}

export function initThemeColor() {
  Object.keys(appConfig.theme).forEach(e => {
    let key = e;
    if (appConfig.theme[key] !== basicColors[key]) {
      localStorage.setItem(key, appConfig.theme[key]);
      basicColors[key] = appConfig.theme[key];
    }
    if (localStorage.getItem(key)) {
      basicColors[key] = localStorage.getItem(key);
      changeThemeColor(localStorage.getItem(key), key);
    }
  });
}
